sizeSignal <- 10000
nSample <- 1000
percentageSample1 = 0.1
propKeep1 = 0.5
percentageSample2 = 0.5
propKeep2 = 0.01
nMonteCarlo <- 10
nOks <- NULL

startTime <- proc.time()[3]
for(i in 1:nMonteCarlo){
	trueSignal = rnorm(sizeSignal)
	estimatedDistances = NULL
	n1 <- round(sizeSignal*percentageSample1)
	n2 <- round(sizeSignal*percentageSample2)
	randomIndex2 <- sample(1:sizeSignal, n2)
	randomIndex1 <- sample(randomIndex2, n1)
	for(x in 1:1000){
		randomSignal = rnorm(sizeSignal)
		ecarts = abs(trueSignal-randomSignal)
		trueDist = mean(ecarts)
		estDist1 = mean(ecarts[randomIndex1])
		estDist2 = mean(ecarts[randomIndex2])
		estimatedDistances <- rbind(estimatedDistances, c(x, trueDist, estDist1, estDist2))
	}

	nKeep1 = sizeSignal*propKeep1
	nKeep2 = sizeSignal*propKeep2
	trueOrder = order(estimatedDistances[,2])[1:2]
	estOrder1 = order(estimatedDistances[,3])[1:nKeep1]
	estOrder2 = order(estimatedDistances[,4])[1:nKeep2]


	trueX = estimatedDistances[trueOrder,1]
	estX1 = estimatedDistances[estOrder1,1]
	estX2 = estimatedDistances[estOrder2,1]

	nOk = 0
	for(value in trueX){
		if(value %in% estX1 & value %in% estX2){
			nOk = nOk+1
		}
	}
	nOk = nOk*100/length(trueX)
	nOks <- c(nOks, nOk)
}
finalTime <- proc.time()[3]
elapsedTime <- finalTime - startTime
print(c('nOk', mean(nOks)))
print(c('elapsedTime', elapsedTime))

